package com.yyx.app1.DiagnosticTools_function.Control.ViewPager.ViewPagerItemControl.threeControl;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yyx.app1.Data.DataInit;
import com.yyx.app1.DiagnosticTools_function.UI.Second_BiochemicalBloodGasAnalysis_Activity_;
import com.yyx.app1.MedicationAssistant_function.Controls.Medication_Activity_Control.drup_listview.Medication_ListView_Adapter;
import com.yyx.app1.MedicationAssistant_function.Controls.SearchDrup_Activity_Control.HotSearch_ListView.HotSearch_ListView_Adapter;
import com.yyx.app1.R;

import org.androidannotations.annotations.AfterTextChange;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;

import java.util.HashMap;

/**
 * Created by yuexing on 2015/9/14.
 */
@EViewGroup(R.layout.view_search)
public class BiochemicalSetOfInspection_View extends LinearLayout {
    private Context context;
    private Biochemical1_ListViewAdapter adapter;

    @ViewById
    TextView view_search_title,view_search_clear_button;

    @ViewById
    EditText view_search_edittext;

    @ViewById
    ListView view_searchdrup_listview;

    public BiochemicalSetOfInspection_View(Context context) {
        super(context);
        this.context=context;
    }

    public BiochemicalSetOfInspection_View(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
    }

    public BiochemicalSetOfInspection_View(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
    }

    @AfterViews
    public void inti()
    {
        view_search_edittext.setText("");
        view_search_edittext.setTextColor(Color.BLACK);
        view_search_edittext.setHint("生化套系检查");
        view_search_title.setText("热门搜索推荐");
        adapter = new Biochemical1_ListViewAdapter(context);
        changeAdapter();
        view_searchdrup_listview.setOverScrollMode(OVER_SCROLL_NEVER);
        view_searchdrup_listview.setDivider(new ColorDrawable(Color.rgb(200, 200, 200)));
        view_searchdrup_listview.setDividerHeight(1);
    }

    @Click
    public void view_search_clear_button()
    {
        new Handler().postDelayed(delete_button_chang, 300);
        view_search_edittext.setText("");
        view_search_clear_button.setTextColor(Color.rgb(100, 100, 100));
        changeAdapter();
        view_search_title.setText("热门搜索推荐");
    }

    Runnable delete_button_chang = new Runnable() {
        @Override
        public void run() {
            view_search_clear_button.setTextColor(Color.rgb(0, 0, 0));
        }
    };

    @AfterTextChange(R.id.view_search_edittext)
    public void edittextchange()
    {
        String str = view_search_edittext.getText().toString();
        if (str.isEmpty())
            return;
        if(new DataInit(context,false,"cxlin").getRemedyInfo0_SQLiteOperation().find(str))
        {
            Intent intent =new Intent(context, Second_BiochemicalBloodGasAnalysis_Activity_.class);
            intent.putExtra("chineseName",str);
            context.startActivity(intent);
        }
        else
        {
            changeAdapter(str);
            view_search_title.setText("搜索结果:" + adapter.getCount() + "种药品");
        }
    }

    public void changeAdapter()
    {
        adapter.findAll();
        view_searchdrup_listview.setAdapter(adapter);
    }

    public void changeAdapter(String str)
    {
        adapter.findAll(str);
        view_searchdrup_listview.setAdapter(adapter);
    }

    @ItemClick(R.id.view_searchdrup_listview)
    public void itemclicked(int position)
    {
        String chineseName = ((HashMap<String, String>) adapter.getItem(position)).get("chineseName");
        view_search_edittext.setText(chineseName);
    }
}
